/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes;

import com.google.inject.Inject;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
public class ActiveDActionBean {
    
    private static Logger LOG = LoggerFactory.getLogger(ActiveDActionBean.class);
    
    private String domain;  
    private String ldapHost;  
    private String searchBase;
        
    public ActiveDActionBean(){
        Configuration conf = new Configuration();
        this.domain = conf.getProperty("activeD.domain");
        this.ldapHost = conf.getProperty("activeD.host");
        this.searchBase = conf.getProperty("activeD.base");
//        this.domain = "iconoffshore.local";
//        this.ldapHost = "ldap://oryxz.dlinkddns.com:389";
//        this.searchBase = "dc=iconoffshore,dc=local";
    }
    
    public boolean authenticateAD(String username, String password){
        boolean ldapUser = false;
        String returnedAtts[] ={ "sn", "givenName", "mail" };  
        String searchFilter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
        
        SearchControls searchCtrls = new SearchControls();
        searchCtrls.setReturningAttributes(returnedAtts);
        searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapHost);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, username+"@"+domain);
        env.put(Context.SECURITY_CREDENTIALS, password);
        
        LdapContext ctxGC = null;
        try{
            ctxGC = new InitialLdapContext(env, null);
            
            NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtrls);
            while(answer.hasMoreElements()){
                SearchResult sr = (SearchResult) answer.next();
                Attributes attrs = sr.getAttributes();
                if(attrs != null){
                    NamingEnumeration ne = attrs.getAll();
                    while(ne.hasMore()){
                        Attribute attr = (Attribute) ne.next();
                        LOG.debug("attr.getID :"+attr.getID()+" = "+attr.get());
                        ldapUser = true;
                    }
                }
            }
        }catch(NamingException ex){
            LOG.error("(ActiveDActionBean) ex :"+ex.getMessage());
            ex.printStackTrace();
        }
        LOG.info("(ActiveDActionBean) ldapUser :"+ldapUser);
        return ldapUser;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getLdapHost() {
        return ldapHost;
    }

    public void setLdapHost(String ldapHost) {
        this.ldapHost = ldapHost;
    }

    public String getSearchBase() {
        return searchBase;
    }

    public void setSearchBase(String searchBase) {
        this.searchBase = searchBase;
    }
}
